//
//  HMRetweetView.swift
//  Weibo
//
//  Created by 李学林 on 15/12/10.
//  Copyright © 2015年 tete. All rights reserved.
//

import UIKit
import SnapKit
class HMRetweetView: UIView {
    // 判断底部约束的变量
    var bottomConstraint: Constraint?
    
    var statusViewModel: HMStatusViewModel?{
        didSet{
            contentLabel.text = statusViewModel?.status?.retweeted_status?.text
            
            // 根据网络数据判断转发微博是否有配图
            self.bottomConstraint?.uninstall()
            if let pic_urls = statusViewModel?.status?.retweeted_status?.pic_urls where pic_urls.count>0 {
                picktureView.hidden = false
                // 在这里可以给pickerView设置数据
                picktureView.pic_urls = pic_urls
                
                
                // 更新约束
                snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(picktureView).offset(HMStatusCellMargin).constraint
                })
                
                
                
            }else{
                picktureView.hidden = true
                // 更新约束
                snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel).offset(HMStatusCellMargin).constraint
                })
            
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    private func setupUI(){
    
        backgroundColor = UIColor(white:0.9,alpha: 1)
       
        // 添加控件
        addSubview(contentLabel)
        
        addSubview(picktureView)
        
        // 添加约束
        // 转发微博正文约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(HMStatusCellMargin)
            make.leading.equalTo(self).offset(HMStatusCellMargin)
        }
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(picktureView).offset(HMStatusCellMargin).constraint
        }
        
        // 转发微博'配图'约束
        picktureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HMStatusCellMargin)
            make.size.equalTo(CGSizeMake(100, 100))
        }
    }
    
    
    // 懒加载控件
    private lazy var contentLabel:UILabel = UILabel(textColor: UIColor.grayColor(), textSize: 15,maxWidth: SCREENW - 2 * HMStatusCellMargin)
    
    // 配图控件
    private lazy var picktureView: HMPictureView = HMPictureView()
}

